<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Zabbix脚本报警介质自定义 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Zabbix脚本报警介质自定义</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Zabbix/">Zabbix</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;zabbix支持mail报警，sms报警，以及自定义报警，用到最多的还是脚本报警，当事件通知到脚本，会传递给脚本三个参数。</p>
<ul>
<li>$1：发送给谁</li>
<li>$2：标题</li>
<li>$3：报警内容</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加媒介类型：Adminstrator→Media Type→Add</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Zabbix%E8%84%9A%E6%9C%AC%E6%8A%A5%E8%AD%A6%E4%BB%8B%E8%B4%A8%E8%87%AA%E5%AE%9A%E4%B9%89/01.jpg?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;那么上面提到的sms.sh放在哪里呢？</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">[root@aliyun-test ~]<span class="comment"># cd /usr/lib/zabbix/alertscripts/</span></div><div class="line">[root@aliyun-test alertscripts]<span class="comment"># vim sms.sh</span></div><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">ALERT_TO=<span class="variable">$1</span></div><div class="line">ALERT_TITLE=<span class="variable">$2</span></div><div class="line">ALERT_BODY=<span class="variable">$3</span></div><div class="line"></div><div class="line"><span class="built_in">echo</span> <span class="variable">$ALERT_TO</span> &gt;&gt; /tmp/sms.log</div><div class="line"><span class="built_in">echo</span> <span class="variable">$ALERT_TITLE</span> &gt;&gt; /tmp/sms.log</div><div class="line"><span class="built_in">echo</span> <span class="variable">$ALERT_BODY</span> &gt;&gt; /tmp/sms.log</div><div class="line">[root@aliyun-test alertscripts]<span class="comment"># chmod +x ./sms.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;上面这个目录在zabbix的server配置段有定义，定义内容如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="comment">### Option: AlertScriptsPath</span></div><div class="line"><span class="comment"># Full path to location of custom alert scripts.</span></div><div class="line"><span class="comment"># Default depends on compilation options.</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># Mandatory: no</span></div><div class="line"><span class="comment"># Default:</span></div><div class="line"><span class="comment"># AlertScriptsPath=$&#123;datadir&#125;/zabbix/alertscripts</span></div><div class="line"></div><div class="line">AlertScriptsPath=/usr/lib/zabbix/alertscripts</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加Actions：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Zabbix%E8%84%9A%E6%9C%AC%E6%8A%A5%E8%AD%A6%E4%BB%8B%E8%B4%A8%E8%87%AA%E5%AE%9A%E4%B9%89/02.jpeg?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改用户的媒体类型：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Zabbix%E8%84%9A%E6%9C%AC%E6%8A%A5%E8%AD%A6%E4%BB%8B%E8%B4%A8%E8%87%AA%E5%AE%9A%E4%B9%89/03.jpeg?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加完了以后，还记得我们之前添加过得nginx.active&gt;10的触发器么。这里去触发一下触发器然后观察event事件。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Zabbix%E8%84%9A%E6%9C%AC%E6%8A%A5%E8%AD%A6%E4%BB%8B%E8%B4%A8%E8%87%AA%E5%AE%9A%E4%B9%89/04.jpeg?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们发现他在发信息呢。看看我们定义的/tmp下的sms.log有没有内容呢：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@aliyun-test ~]<span class="comment"># tail -F /tmp/sms.log </span></div><div class="line"><span class="built_in">test</span>@admin.com</div><div class="line">PROBLEM: 当nginx的动态连接数大于10的话我就报警</div><div class="line"> Original event ID: 454*:*UNKNOWN*): *UNKNOWN*1</div><div class="line"><span class="built_in">test</span>@admin.com</div><div class="line">PROBLEM: 当nginx的动态连接数大于10的话我就报警</div><div class="line"> Original event ID: 455*:*UNKNOWN*): *UNKNOWN*9</div><div class="line"><span class="built_in">test</span>@admin.com</div><div class="line">PROBLEM: 当nginx的动态连接数大于10的话我就报警</div><div class="line"> Original event ID: 456*:*UNKNOWN*): *UNKNOWN*4</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以发现确实写入到文本了。那么在实际的应用中我们就可以使用传过来的$1 $2 $3这三个来灵活的去处理了。只要获取到这个内容我们就可以按照自己的要求去处理了，不管你是发短信还是发邮件都是OK的。这取决于你的脚本到底是怎么写的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;关于短信发送的相关信息我们可以参考<a href="https://dayu.aliyun.com/" target="_blank" rel="external">阿里大于</a>，在<a href="https://api.alidayu.com/doc2/apiDetail?apiId=25450" target="_blank" rel="external">阿里大于的帮助平台</a>提供有短信发送的API以及相关参数。当然也可以使用Curl来完成，不过就是稍微有点麻烦就是了。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;实践需要注意的一些内容：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Actions中定义的内容都是我们要收到的内容，如果你使用邮件这应该问题不大，不过你要是使用短信的话你就需要注意点了。因为在国内一条短信的长度是有限制的，搞不好一条短信发不完还得分开好几条发送，如果告警多了这个花销也是不可小看的。因此我们可以把actions改造一下，其实获取关键信息就足够了，定位机器，定位故障等。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Zabbix%E8%84%9A%E6%9C%AC%E6%8A%A5%E8%AD%A6%E4%BB%8B%E8%B4%A8%E8%87%AA%E5%AE%9A%E4%B9%89/05.jpeg?raw=true" alt=""></figure></p>

	

	
		<span class="different-posts"><a href="/2017/10/12/Zabbix/7. Zabbix脚本报警介质自定义/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
